package com.example.auth.utils;

import cn.dev33.satoken.stp.StpUtil;
import com.example.auth.model.domain.SysUser;

import java.util.Optional;

/**
 * @Description
 * @Author xijie.wang
 * @Date created on 2022/3/15
 */
public class LoginUtil {

	/**
	 * 获取当前登录的用户信息
	 *
	 * @return
	 */
	public static SysUser getLoginUser() {

		if (StpUtil.getSession().get("user") != null) {
			return (SysUser) StpUtil.getSession().get("user");
		}

		return null;
	}

	/**
	 * 获取当前登录的渠道id
	 *
	 * @return
	 */
	public static Integer getLoginUserId() {
		return Optional.ofNullable(getLoginUser())
				.map(SysUser::getId)
				.orElse(null);
	}

	/**
	 * 获取当前登录的渠道id
	 *
	 * @return
	 */
	public static Integer getNoLoginUserId() {
		if (StpUtil.isLogin()) {
			return Optional.ofNullable(getLoginUser())
					.map(SysUser::getId)
					.orElse(null);
		}
		return null;
	}
}
